package com.example.polarity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class Magnet extends View 
{
	
	public Bitmap negativeMagnet;
	public Bitmap positiveMagent;

	public int magnetType;
	
	public int width;
	public int height;
	
	private float x;   // the X coordinate
	private float y;   // the Y coordinate
	
	public boolean start = false;
	public Canvas magnetCanvas; 
	private static final String TAG = MainThread.class.getSimpleName();
	
	public Magnet(Context context,View parent, int type)
	{
		super(context);
		
		this.magnetType = type;
		this.negativeMagnet = BitmapFactory.decodeResource(parent.getResources(), R.drawable.neg_magnet);
		this.positiveMagent = BitmapFactory.decodeResource(parent.getResources(), R.drawable.pos_magnet);
		
		 //its the inital create of the magnet so we need to set the x and y values to the proper object
		this.x = parent.getLeft();
		this.y = parent.getTop();

		
		this.width = positiveMagent.getWidth();
		this.height = positiveMagent.getHeight();
		
	}
	
 
	public boolean UpdatePosition(MotionEvent event)
	{
		
		float right = this.x + this.width;
		float left  = this.x;
		float top   = this.y;
		float bottom = this.y + this.height;
		
		if(event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom)
		{
			Log.d(TAG, "mouse x and y = "  + event.getX() + " -- " + event.getY());
			Log.d(TAG, "mag y  = "  + this.y);
			return true;
		}
		else
			return false;
	}
	
	public void setPosition(MotionEvent event)
	{
		this.x = event.getX();
		this.y = event.getY();
	}
	 
	@SuppressLint("NewApi")
	public void DrawMagnet(Canvas canvas, float X, float Y)
	{
		this.x = X;
		this.y = Y;
	}
	 
	 public void Draw(Canvas canvas)
	 {
		 if(this.magnetType == 1)
			 canvas.drawBitmap(negativeMagnet, this.x,this.y, null);
		 else
			 canvas.drawBitmap(positiveMagent, this.x,this.y, null);
	 }
	 

	
	public float getMagnetX() 
	{
		return x;
	}

	public float getMagnetY() 
	{
		return y;
	}

}
